package com.hyy.taobao.controller;

import com.hyy.taobao.api.IUserService;
import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updateUser")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;utf-8");
        HttpSession session = req.getSession();
        User myUser = (User) session.getAttribute("myUser");
        String newUserPwd1 = req.getParameter("newUserPwd1");
        String newUserPwd2 = req.getParameter("newUserPwd2");
        if (newUserPwd1.equals(newUserPwd2)) {
            User user = new User(myUser.getUserName(), newUserPwd1);
            IUserService iUserService = new UserServiceImpl();
            boolean judgement = iUserService.updateUser(user);
            if (judgement) {
                req.setAttribute("msg", "密码修改成功，请重新登录");
                req.getRequestDispatcher("Login.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "密码修改失败，请重新尝试");
                req.getRequestDispatcher("updateUserWeb.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("msg", "两次输入的密码不一样，请重新输入 ");
            req.getRequestDispatcher("updateUserWeb.jsp").forward(req, resp);
        }
    }
}
